<template>
  <div class="page" @scroll="paperScroll" ref="page">
    <div class="head">
      <h1>{{$t('广场')}}</h1>
      <div class="tool">
        <group gutter="0" class="search">
          <x-input @on-enter="search(id)" @on-change="onChange" type="number" v-model="id" :placeholder="$t('搜索') + ' ID'" :show-clear="false"></x-input>
          <div class="search-icon" @click="search(id)">
            <icon type="search"></icon>
          </div>
        </group>
        <div class="option" @click="showFilter = true"></div>
      </div>
    </div>
    <sticky>
      <div class="filter">
        <ul class="status">
          <li v-for="(item, index) in category" :class="{'active': item.value == params.status}" @click="changeCategory(item.value)" :key="index">{{$t(item.name)}}</li>
        </ul>
        <ul class="order" v-show="params.status != 3">
          <li :class="{'active': item.value == params.orderBy,'up': params.sort == 0}" @click="changeOrder(item.value)" v-for="(item, index) in order" :key="index">{{$t(item.name)}}</li>
        </ul>
      </div>
    </sticky>
    <div class="panel" v-if="params.status != 3">
      <div class="item" @click="toMonkey(item.id)" v-for="(item,index) in homeList" :key="index">
        <div class="img">
          <img :src="item.facade" :style="{background: item.background}">
          <div class="id"># {{ item.id }}</div>
          <div class="gen">{{ item.generation }} {{ $t('代') }}</div>
          <div class="info">
            <p>{{ item.bearNum }} {{ $t('次') }}</p>
            <p>
              <span>{{ item.grow | toFix(2) }}/</span>
              <span>{{ item.bear | toFix(2) }}/</span>
              <span>{{ item.makeMoney | toFix(2) }} · {{ item.weight | toFix(2) }}kg</span>
            </p>
          </div>
        </div>
        <div class="price">
          <i class="ico" :class="{'sell':item.type == 1,'love':item.type == 2}"></i>
          <span>{{ item.price }} 猴毛</span>
        </div>
      </div>
      <infinite-loading @infinite="initData" :distance="500" ref="infiniteLoading1">
        <span slot="no-more"></span>
        <div slot="no-results">
          <br>
          <empty></empty>
        </div>
        <spinner slot="spinner" type="lines"></spinner>
      </infinite-loading>
    </div>
    <!-- 猴趣拍 -->
    <div class="panel" v-if="params.status == 3">
      <router-link tag="div" class="item" :to="`/auction?id=${item.id}`" v-for="(item,index) in sellList" :key="index">
        <div class="img">
          <img :src="item.monkey.facade" :style="{background: item.monkey.background}">
        </div>
        <div class="price">
          <span>{{ item.coin }} {{ item.monkey.nickName || '猴毛' }} </span>
        </div>
      </router-link>
      <empty v-if="sellList.length == 0"></empty>
    </div>
    <!-- 复杂筛选 -->
    
  </div>
</template>
<script>
  import {
    Group,
    XInput,
    Sticky,
    Spinner,
    Icon,
    TransferDom,
    Popup,
    XButton
  } from 'vux'
  import empty from './common/empty.vue'
  import InfiniteLoading from 'vue-infinite-loading';
  export default {
    directives: {
      TransferDom
    },
    components: {
      Group,
      XInput,
      Sticky,
      empty,
      InfiniteLoading,
      Spinner,
      Icon,
      Popup,
      XButton
    },
    data() {
      return {
        category: [{
            name: '集市',
            value: '1'
          },
          {
            name: '鹊桥',
            value: '2'
          },
          {
            name: '猴趣拍',
            value: '3'
          }
        ],
        order: [{
            name: '价格',
            value: '1'
          },
          {
            name: '代数',
            value: '2'
          },
          {
            name: '体重',
            value: '3'
          },
          {
            name: '生育',
            value: '4'
          },
        ],
        value: '',
        homeList: [],
        sellList: [],
        params: {
          orderBy: 1, //1:代数,2:体重,3:收益,4:生育
          sort: '0', //0:升 1:降
          status: 1, //0:正常 1: 出售 2:繁殖,不传值显示全部
          current: 1,
          id: '',
          gen: '',
          startGen: '',
          endGen: '',
          startGrow: '',
          endGrow: '',
          startBear: '',
          endBear: '',
          startMakeMoney: '',
          endMakeMoney: '',
          startBearNum: '',
          endBearNum: '',
          startWeight: '',
          endWeight: '',
        },
        id: '',
        scrollTop_market: 0,
        showFilter: false
      }
    },
    mounted() {
      this.$nextTick(function() {
        this.initSellData();
      });
    },
    activated() {
      this.$refs.page.scrollTop = this.$store.state.scrollTop_market;
    },
    methods: {
      initData($state) {
        this.$http
          .post(`/game/filterHomePage`, this.params)
          .then(res => {
            if (res.code == 200) {
              // this.params.gen = '';
              if (res.result.total > 0) {
                this.homeList = this.homeList.concat(res.result.records);
                $state.loaded();
                if (res.result.current < res.result.pages) {
                  this.params.current += 1;
                } else {
                  $state.complete();
                }
              } else {
                $state.complete();
              }
            } else {}
          })
          .catch(function(error) {});
      },
      resetFilter() {
        this.params.startGen = '';
        this.params.endGen = '';
        this.params.startGrow = '';
        this.params.endGrow = '';
        this.params.startBear = '';
        this.params.endBear = '';
        this.params.startMakeMoney = '';
        this.params.endMakeMoney = '';
        this.params.startBearNum = '';
        this.params.endBearNum = '';
        this.params.startWeight = '';
        this.params.endWeight = '';
      },
      enterFilter() {
        this.$nextTick(() => {
          this.resetData()
          this.showFilter = false
        });
      },
      paperScroll(e) {
        this.scrollTop_market = e.srcElement.scrollTop
      },
      toMonkey(id) {
        this.$store.commit('scrollTop', {
          name: 'scrollTop_market',
          value: this.scrollTop_market
        })
        this.$router.push(`/monkey/${id}`)
      },
      initSellData() {
        this.$http
          .get(`/game/listAuction`)
          .then(res => {
            if (res.code == 200) {
              this.sellList = res.result
            } else {}
          })
          .catch(function(error) {});
      },
      // 筛选目录
      changeCategory(value) {
        this.params.status = value
        if (value != 3) {
          this.$nextTick(() => {
            this.resetData()
          });
        } else {
          this.initSellData()
        }
      },
      changeOrder(order) {
        //点击同一个
        if (order == this.params.orderBy) {
          this.params.sort = this.params.sort == 0 ? 1 : 0;
        } else {
          this.params.orderBy = order;
          this.params.sort = 1
        }
        this.$nextTick(() => {
          this.resetData()
        });
      },
      resetData() {
        this.params.current = 1;
        this.homeList = []
        this.$refs.infiniteLoading1.$emit('$InfiniteLoading:reset');
      },
      search(value) {
        if (value != '') {
          this.params.id = value;
          this.$nextTick(() => {
            this.resetData()
          });
        }
      },
      onChange(value) {
        if (value == '') {
          this.params.id = value;
          this.$nextTick(() => {
            this.resetData()
          });
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  .page {
    position: relative;
  }
  .panel {
    padding: 12px 4px;
    overflow: hidden;
  }
  .head {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .swiper-container {
    overflow: visible;
  }
  .item {
    display: inline-block;
    vertical-align: middle;
    width: 33.33%; // min-height: 150px;
    border: 4px solid transparent;
    line-height: 1;
    overflow: hidden;
    min-height: 90px;
    .img {
      position: relative;
      font-weight: 300;
      width: 100%;
      min-height: 90px;
      background-color: #f9f7fc;
      font-size: 10px;
      border-radius: 4px;
      overflow: hidden;
      @media only screen and (min-width: 375px) {
        font-size: 10px;
      }
      @media only screen and (min-width: 414px) {
        font-size: 12px;
      }
      @media only screen and (min-width: 768px) {
        font-size: 16px;
      }
      img {
        width: 100%;
      }
      .id {
        position: absolute;
        color: $grey;
        background-color: rgba(255, 255, 255, .8);
        left: 0;
        top: 6px;
        padding: 3px 5px 3px 0;
        border-radius: 0 10px 10px 0;
      }
      .gen {
        position: absolute;
        right: 5px;
        top: 8px;
      }
      .info {
        position: absolute;
        left: 2px;
        bottom: 2px; // line-height: 14px;
        color: #000;
        width: 100%;
        white-space: nowrap;
        padding: 2px;
        p {
          width: 100%;
          overflow: hidden;
          text-overflow: ellipsis;
          line-height: 1.2;
        }
      }
    }
    .price {
      text-align: center;
      font-size: 12px;
      padding: 5px 10px;
      color: $green;
      white-space: nowrap;
      @media only screen and (min-width: 375px) {
        font-size: 10px;
      }
      @media only screen and (min-width: 414px) {
        font-size: 12px;
      }
      @media only screen and (min-width: 768px) {
        font-size: 16px;
      }
    }
  }
  .swiper-pagination {
    bottom: -30px;
  }
  .filter {
    display: flex;
    justify-content: space-between;
    white-space: nowrap; // flex-wrap: wrap;
    font-size: 12px;
    background: #fff;
    height: 40px;
    line-height: 40px;
    padding: 0 12px;
    margin: 0 8px 12px;
    border-radius: 4px;
    box-shadow: 3px 3px 10px 0px rgba(158, 158, 158, 0.4);
    overflow-x: scroll;
    overflow-y: hidden;
    &::-webkit-scrollbar {
      display: none;
    }
    ul {
      display: flex;
      align-items: center;
      li {
        cursor: pointer;
      }
    }
    .status {
      
      color: $grey;
      li {
        transition: all .3s;
      }
      li:not(:last-child) {
        margin-right: 12px;
      }
      .active {
        color: $green;
        font-size: 14px;
        border-bottom: 2px solid $green;
      }
    }
    .order {
      margin-left: 20px;
      color: $link;
      li+li {
        margin-left: 12px;
      }
      .active:after {
        content: '';
        display: inline-block;
        vertical-align: middle;
        margin-top: -2px;
        width: 12px;
        height: 12px;
        background: url() no-repeat 50%;
        background-size: cover;
      }
      .active.up:after {
        transform: rotate(-180deg)
      }
    }
  }
  .tool {
    display: flex;
    .option {
      width: 28px;
      height: 28px;
      margin-left: 5px;
      background: url() no-repeat 50%;
      background-size: 16px 16px;
    }
  }
  .filters {
    overflow: hidden;
    background: #fbf9fe;
    &::-webkit-scrollbar {
      display: none;
    }
    .wrap {
      padding-bottom: 60px;
      height: 100%;
      overflow-y: scroll;
      padding: 12px;
    }
    .button {
      position: absolute;
      left: 0;
      bottom: 0;
      width: 100%;
      display: flex;
      .btn {
        width: 50%;
        height: 40px;
        margin: 0;
        border: 0;
        border-radius: 0;
        &:after {
          display: none;
        }
      }
    }
  }
</style>

<style lang="scss">
  .my-bullet {
    color: #dfdfdf;
    font-size: 14px;
    margin: 0 5px;
    transition: .3s;
    display: inline-block;
    vertical-align: middle;
  }
  .my-bullet-active {
    font-size: 18px;
    color: #333;
  }
  .swiper-pagination-bullet {
    color: #fff;
    font-size: 10px;
    width: 16px;
    height: 16px;
    padding: 4px;
    line-height: 16px;
    text-align: center;
    box-sizing: content-box;
  }
  .search {
    position: relative;
    .weui-cells {
      // background-color: transparent;
      &:before {
        display: none;
      }
      &:after {
        display: none;
      }
      border: 1px solid #E6E6EA;
      border-radius: 50px;
      width: 120px;
      height: 28px;
      font-size: 14px;
      overflow: hidden;
      .weui-cell {
        height: 100%;
        padding: 0 5px;
        .weui-cell__bd {
          height: 100%;
          display: flex;
          align-items: center;
          .weui-input {
            width: 100%; // height: 100%;
            padding-left: 8px;
            &::-webkit-input-placeholder {
              color: #ccc;
            }
          }
        }
      }
    }
    .search-icon {
      position: absolute;
      right: 0;
      top: 0;
      width: 28px;
      height: 100%;
      line-height: 28px;
      .weui_icon_search {
        position: relative;
        top: -2px;
      }
    }
  }
  .input-range {
    .weui-cells {
      display: flex;
      &:before {
        display: none;
      }
      &:after {
        display: none;
      }
      .weui-cell {
        background: #eeeff1;
        padding: 5px 10px;
        border-radius: 5px;
        &:before {
          display: none;
        }
        input {
          text-align: center;
        }
      }
    }
    .weui-cells__title {
      padding-left: 0;
      color: #666;
      font-weight: 500;
    }
    .dash {
      // width: 80px;
      padding: 0 5px;
      display: flex;
      align-items: center;
      justify-content: center;
      background: #fbf9fe;
      &:before {
        content: '';
        width: 20px;
        height: 1px;
        background: #000;
      }
    }
  }
</style>
